import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:practice/res/color.dart';

///网络请求时不同状态脚手架
class RequestScaffold extends StatelessWidget {
  final RequestStatus status;
  final Widget child;
  final Widget? emptyWidget;
  final Widget? loadingWidget;
  final Widget? errorWidget;
  final Widget? noDataWidget;

  const RequestScaffold(
      {Key? key,
      required this.status,
      required this.child,
      this.emptyWidget,
      this.loadingWidget,
      this.errorWidget,
      this.noDataWidget})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    switch (status) {
      case RequestStatus.empty:
        return emptyWidget ?? Container();
      case RequestStatus.loading:
        return loadingWidget ??
            Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: const <Widget>[
                  SpinKitCircle(
                    color: Colour.text_3,
                    size: 32,
                  ),
                  Padding(padding: EdgeInsets.all(2)),
                  Text(
                    "正在加载",
                    style: TextStyle(fontSize: 12, color: Colour.text_3),
                  ),
                ],
              ),
            );
      case RequestStatus.success:
        return child;
      case RequestStatus.error:
        return errorWidget ??
            Center(
              child: Text("请求异常"),
            );
      case RequestStatus.noData:
        return noDataWidget ??
            Center(
              child: Text("暂无数据"),
            );
      default:
        return Container();
    }
  }
}

enum RequestStatus {
  loading,
  success,
  noData,
  error,
  empty,
}

///下拉刷新控件状态
enum RefreshAction {
  refreshSuccess,
  refreshFail,
  loadingSuccess,
  loadingFail,
  loadingNoData,
}
